from flask import jsonify, request

from flask import request, jsonify
from datetime import datetime
from decimal import Decimal
from commons.controller.base_controller import BaseController
from travel_service.travel.service.reimbursement_service import ReimbursementService


class ReimbursementController(BaseController):
    def __init__(self):
        self.reimbursement_service = ReimbursementService()

    def get_reimbursement_by_id(self):
        """
        根据报销ID获取报销信息。
        """
        # 从请求体解析参数
        data = request.get_json()
        reimbursement_id = data.get("userId")

        return BaseController.get_success_response(self,
                                                   self.reimbursement_service.get_reimbursement_by_id(reimbursement_id))

    def get_reimbursements_by_id_list(self):
        """
        根据ID列表获取报销信息。
        """
        # 从请求体解析参数
        data = request.get_json()
        id_list = data.get("idList")
        reimbursements = self.reimbursement_service.get_reimbursements_by_id_list(
            id_list)

        return jsonify([reimbursement.to_dict() for reimbursement in reimbursements])

    def add_reimbursement(self):
        """
        添加新的报销记录
        """
        # 从请求体解析参数
        data = request.get_json()

        # 获取并验证必要的参数
        apply_date_str = data.get("applyDate")
        food_cost = data.get("foodCost")
        transport_cost = data.get("transportCost")
        hotel_cost = data.get("hotelCost")
        detail = data.get("detail")
        travel_id = data.get("travelId")

        # 参数验证
        if not all([apply_date_str, food_cost, transport_cost, hotel_cost, detail, travel_id]):
            return jsonify({"error": "Missing required fields"}), 400

        try:
            # 转换applyDate为datetime格式
            # Assuming "YYYY-MM-DD" format
            apply_date = datetime.strptime(apply_date_str, "%Y-%m-%d")

            # 转换 BigDecimal（传输为字符串类型）为 Decimal
            food_cost = Decimal(food_cost)
            transport_cost = Decimal(transport_cost)
            hotel_cost = Decimal(hotel_cost)

            # 创建Reimbursement对象
            reimbursement = {
                "food_cost": food_cost,
                "transport_cost": transport_cost,
                "hotel_cost": hotel_cost,
                "apply_date": apply_date,
                "detail": detail,
                "travel_id": travel_id
            }

            # 调用服务层添加报销记录
            self.reimbursement_service.add_reimbursement(reimbursement)

            # 返回成功响应
            return jsonify({"message": "Reimbursement added successfully"}), 200

        except ValueError as e:
            # 如果日期或数字转换失败
            return jsonify({"error": str(e)}), 400

    def get_reimbursement_by_travel_id(self):
        """
        根据报销ID获取报销信息。
        """
        # 从请求体解析参数
        data = request.get_json()
        travel_id = data.get("travelId")

        return BaseController.get_success_response(self,self.reimbursement_service.get_reimbursement_by_travel_id(travel_id))

